package com.ruoyi.web.controller.api.recipe;

import com.ruoyi.api.domain.recipe.DishMealEntity;
import com.ruoyi.api.domain.recipe.DishMealItemEntity;
import com.ruoyi.common.annotation.NoLoginRequired;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.recipe.domain.RecipeDishMeal;
import com.ruoyi.recipe.service.IRecipeDishMealService;
import com.ruoyi.recipe.service.IRecipeDishService;
import com.ruoyi.web.controller.api.APIController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("宝宝辅食")
@RestController
@RequestMapping("/api/meal")
@NoLoginRequired
public class DishMealController extends APIController {

    @Autowired
    private IRecipeDishService recipeDishService;

    @Autowired
    private IRecipeDishMealService recipeDishMealService;

    @ApiOperation("获取宝宝辅食列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dishId", value = "菜品id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dishName", value = "宝宝月份", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "monthAge", value = "宝宝月份+天数", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/list")
    public TableDataInfo list(@RequestParam(value = "dishId", required = false, defaultValue = "0") Long dishId, @RequestParam(value = "dishName", required = false, defaultValue = "") String dishName, @RequestParam(value = "monthAge", required = false, defaultValue = "") String monthAge) {
        RecipeDishMeal dishMealEntity = new RecipeDishMeal();
        if (dishId > 0) {
            dishMealEntity.setDishId(dishId);
        }
        if (StringUtils.isNotEmpty(dishName)) {
            dishMealEntity.setDishName(dishName.trim());
        }
        if (StringUtils.isNotEmpty(monthAge)) {
            dishMealEntity.setMonthAge(monthAge.trim());
        }
        dishMealEntity.setStatus("0");
        List<RecipeDishMeal> mealList = recipeDishMealService.selectRecipeDishMealList(dishMealEntity);
        List<DishMealEntity> mealEntityList = new ArrayList<>();
        String baseUrl = getBaseUrl();
        for (RecipeDishMeal meal : mealList) {
            DishMealEntity entity = mealEntityList.stream().filter(e -> e.getMonthAge().equals(meal.getMonthAge())).findFirst().orElse(null);
            if (entity == null) {
                entity = new DishMealEntity();
                BeanUtils.copyBeanProp(entity, meal);
                if (StringUtils.isNotEmpty(meal.getDishPic()) && !StringUtils.ishttp(meal.getDishPic())) {
                    String fullDishPic = baseUrl + meal.getDishPic();
                    entity.setDishPic(fullDishPic);
                }
                mealEntityList.add(entity);
            }
            if (entity.getDetailContent() == null) {
                entity.setDetailContent(new ArrayList<>());
            }
            DishMealItemEntity itemEntity = new DishMealItemEntity();
            BeanUtils.copyBeanProp(itemEntity, meal);
            entity.getDetailContent().add(itemEntity);
        }
        return getDataTable(mealEntityList);
    }
}
